package com.darker.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.darker.reggie.common.BaseContext;
import com.darker.reggie.common.R;
import com.darker.reggie.entity.ShoppingCart;
import com.darker.reggie.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;


@Slf4j
@RestController
@RequestMapping("/shoppingCart")
public class ShoppingCartController {
    @Autowired
    ShoppingCartService shoppingCartService;

    //查询购物车
    @GetMapping("/list")
    public R<List<ShoppingCart>> list(){
        LambdaQueryWrapper<ShoppingCart> queryWrapper=new LambdaQueryWrapper<>();

        queryWrapper.eq(ShoppingCart::getUserId,BaseContext.getCurrentId())
                .orderByAsc(ShoppingCart::getCreateTime);

        List<ShoppingCart> shoppingCarts = shoppingCartService.list(queryWrapper);
        return R.success(shoppingCarts);
    }

    //添加购物车
    @PostMapping("/add")
    public R<ShoppingCart> add(@RequestBody ShoppingCart shoppingCart){
        //因为传过来的数据没有用户的ID  需要标识那个用户点的菜=>设置ID
        Long userid = BaseContext.getCurrentId();
        shoppingCart.setUserId(userid);

        LambdaQueryWrapper<ShoppingCart> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId,userid);
        //判断 用户点的是菜品
        if(shoppingCart.getDishId()!=null){
            queryWrapper.eq(ShoppingCart::getDishId,shoppingCart.getDishId());
        }
        //判断 用户点的是套餐
        if(shoppingCart.getSetmealId()!=null){
            queryWrapper.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        }
        ShoppingCart shoppingCart1 = shoppingCartService.getOne(queryWrapper);
        //判断菜品是否已经在购物车
        if(shoppingCart1==null){
            //不在购物车 将菜品保存进购物车
            shoppingCart.setNumber(1);//增加数量
            shoppingCartService.save(shoppingCart);
            shoppingCart1=shoppingCart;
        }else{
            //在购物车 使菜品数量+1
            Integer number = shoppingCart1.getNumber();
            shoppingCart1.setNumber(number+1);
            shoppingCartService.updateById(shoppingCart1);
        }
        shoppingCart1.setCreateTime(LocalDateTime.now());
        return R.success(shoppingCart1);
    }

    //清空购物车
    @DeleteMapping("/clean")
    public R<String> clean(){
        LambdaQueryWrapper<ShoppingCart> queryWrapper=new LambdaQueryWrapper<>();

        queryWrapper.eq(ShoppingCart::getUserId,BaseContext.getCurrentId());
        shoppingCartService.remove(queryWrapper);
      return   R.success("清空购物车成功");
    }
    //减少套餐 菜品 数量
    @PostMapping("/sub")
    public R<ShoppingCart> sub(@RequestBody ShoppingCart shoppingCart){
        //获取用户ID
        Long id = BaseContext.getCurrentId();
        LambdaQueryWrapper<ShoppingCart> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId,id);

        //表示需要处理的的是套餐
        if(shoppingCart.getSetmealId()!=null){
            queryWrapper.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        }
        //表示需要处理的是菜品
        if(shoppingCart.getDishId()!=null){
            queryWrapper.eq(ShoppingCart::getDishId,shoppingCart.getDishId());
        }
        //获取当前选中的商品信息
        ShoppingCart userShoppingcart = shoppingCartService.getOne(queryWrapper);

        Integer number = userShoppingcart.getNumber();
        if(number==1){
            //从购物车中删除商品
            shoppingCartService.remove(queryWrapper);
        }
        //减少商品数量
        userShoppingcart.setNumber(number-1);
        shoppingCartService.updateById(userShoppingcart);
        return R.success(userShoppingcart);
    }
}
